package com.wlz.authorization.config;

import com.wlz.authorization.handler.MyAccessDeniedHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 *  Authorization 授权
 *  方法授权
 *     基于注解的访问控制
 */
@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true,securedEnabled = true,prePostEnabled = true)
//@EnableWebSecurity 基于springboot 可以不用
public class WebSecurityMethodConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin();  //表单登录

        //授权
        http.authorizeRequests()
//                .antMatchers("/admin/demo").permitAll() // 不用认证
                .anyRequest().authenticated();  //  必须认证

        http.exceptionHandling().accessDeniedHandler(new MyAccessDeniedHandler()); // 支持自定义权限受限处理，需要实现 AccessDeniedHandler接口
        http.csrf().disable();//csrf关掉

    }

    @Bean
    public PasswordEncoder passwordEncoder(){
//        return NoOpPasswordEncoder.getInstance();
        return new BCryptPasswordEncoder();
    }


}
